package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.adapter.ActOrderDetailGVAdapter;
import com.landeb.shop.huo.bean.BeanActOrderDetail;
import com.landeb.shop.huo.httpRequest.ActOrderDetailRequest;
import com.landeb.shop.huo.httpRequest.UserConfirmationRequest;
import com.landeb.shop.huo.view.FixedGridView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class OrderDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private String mOrderId;

    private SwipeRefreshLayout mSRL;
    private Button btnService;
    private TextView mOrderNum, mOrderStatus, mExpressName, mTrackingNum, mRecipients, mRecipientsNum, mAddress, mShopName, mPayWay, mInvoiceTitle, mInvoiceInfo, mTotalPrie, mCarriage, mRealPrice, mOrderTime;
    private LinearLayout btnBack, mTrackingNumCtrl, llUnPay, llPay, llSend, llCancel, llFinish;
    private FixedGridView mGridView;

    private List<BeanActOrderDetail.DataBean.GoodsBean> mGoodsBeen;
    private ActOrderDetailGVAdapter mAdapter;

    private OkHttpClient mClient;
    private Retrofit mRetrofit;
    private ActOrderDetailRequest mRequest;
    private String mUrl;
    private boolean onRefreshing = false;

    public static void toAct(Context context) {
        Intent intent = new Intent(context, OrderDetailActivity.class);
        context.startActivity(intent);
    }

    public static void toAct(Context context, String orderId) {
        Intent intent = new Intent(context, OrderDetailActivity.class);
        intent.putExtra("orderId", orderId);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);

        Intent intent = getIntent();
        mOrderId = intent.getStringExtra("orderId");

        mClient = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(mClient)
                .build();
        mRequest = mRetrofit.create(ActOrderDetailRequest.class);

        initView();
        initData();
        initEvent();

        refresh();
    }

    private void initEvent() {

        mSRL.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        mSRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO: 2016/11/26
                refresh();
            }
        });

        btnBack.setOnClickListener(this);
        btnService.setOnClickListener(this);
        mShopName.setOnClickListener(this);

        mAdapter = new ActOrderDetailGVAdapter(this, mGoodsBeen);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO: 2016/11/26 gv点击事件
                String goodID = mGoodsBeen.get(position).getGoods_id();
                String title = mGoodsBeen.get(position).getName();
                String price = mGoodsBeen.get(position).getPrice();
                GoodDetailActivity.toAct(OrderDetailActivity.this, goodID, title, price);

            }
        });


    }

    private void refresh() {

        mSRL.setRefreshing(true);

        if (!onRefreshing) {

            onRefreshing = true;

            mRequest.getOrderDetail(mOrderId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BeanActOrderDetail>() {
                        @Override
                        public void onCompleted() {
                            onRefreshing = false;
                            mSRL.setRefreshing(false);
                            Log.e("getOrderDetail", "onCompleted: ");
                        }

                        @Override
                        public void onError(Throwable e) {
                            onRefreshing = false;
                            mSRL.setRefreshing(false);

                            Log.e("getOrderDetail", "onError: " + e.toString());

                        }

                        @Override
                        public void onNext(BeanActOrderDetail beanActOrderDetail) {

                            if (beanActOrderDetail.getStatus() == 1) {

                                mGoodsBeen.clear();
                                mGoodsBeen.addAll(beanActOrderDetail.getData().getGoods());
                                mAdapter.notifyDataSetChanged();

                                mOrderNum.setText(beanActOrderDetail.getData().getId());
                                mOrderStatus.setText(beanActOrderDetail.getData().getStatus_name());
                                mExpressName.setText(beanActOrderDetail.getData().getExpress_name() + ":");
                                mTrackingNum.setText(beanActOrderDetail.getData().getExpress_number());
                                mRecipients.setText(beanActOrderDetail.getData().getRealname());
                                mRecipientsNum.setText(beanActOrderDetail.getData().getMobile());
                                mAddress.setText(beanActOrderDetail.getData().getProvince() +
                                        beanActOrderDetail.getData().getCity() +
                                        beanActOrderDetail.getData().getCountry() +
                                        beanActOrderDetail.getData().getTown());
                                mShopName.setText(beanActOrderDetail.getData().getStorename());
                                mPayWay.setText(beanActOrderDetail.getData().getPayway_name());
                                mInvoiceTitle.setText(beanActOrderDetail.getData().getBill_title());
                                mInvoiceInfo.setText(beanActOrderDetail.getData().getBill_content());
                                mTotalPrie.setText("¥" + beanActOrderDetail.getData().getMoney());
                                mCarriage.setText("¥" + beanActOrderDetail.getData().getExpress_money());
                                mRealPrice.setText("¥" + beanActOrderDetail.getData().getMoney());
                                mOrderTime.setText("下单时间:" + beanActOrderDetail.getData().getCtime_date());
                            } else {

                                Toast.makeText(OrderDetailActivity.this, beanActOrderDetail.getInfo(), Toast.LENGTH_SHORT).show();

                            }

                        }
                    });

        }

    }

    private void initData() {
        mGoodsBeen = new ArrayList<>();
    }

    private void initView() {

        mSRL = (SwipeRefreshLayout) findViewById(R.id.act_order_detail_srl);

        btnBack = (LinearLayout) findViewById(R.id.act_order_detail_btn_back);
        btnService = (Button) findViewById(R.id.act_order_detail_btn_service);

        mOrderNum = (TextView) findViewById(R.id.act_order_detail_tv_orderNum);
        mOrderStatus = (TextView) findViewById(R.id.act_order_detail_tv_orderStatus);
        mExpressName = (TextView) findViewById(R.id.act_order_detail_tv_expressName);
        mTrackingNum = (TextView) findViewById(R.id.act_order_detail_tv_trackingNum);
        mRecipients = (TextView) findViewById(R.id.act_order_detail_tv_recipients);
        mRecipientsNum = (TextView) findViewById(R.id.act_order_detail_tv_recipientsNum);
        mAddress = (TextView) findViewById(R.id.act_order_detail_tv_address);
        mShopName = (TextView) findViewById(R.id.act_order_detail_tv_shopName);
        mPayWay = (TextView) findViewById(R.id.act_order_detail_tv_payWay);
        mInvoiceTitle = (TextView) findViewById(R.id.act_order_detail_tv_invoiceTitle);
        mInvoiceInfo = (TextView) findViewById(R.id.act_order_detail_tv_invoiceInfo);
        mTotalPrie = (TextView) findViewById(R.id.act_order_detail_tv_totalPrice);
        mCarriage = (TextView) findViewById(R.id.act_order_detail_tv_carriage);
        mRealPrice = (TextView) findViewById(R.id.act_order_detail_tv_realPrice);
        mOrderTime = (TextView) findViewById(R.id.act_order_detail_tv_time);

        mTrackingNumCtrl = (LinearLayout) findViewById(R.id.act_order_detail_ll_trackingNum);
        mGridView = (FixedGridView) findViewById(R.id.act_order_detail_goods_gv);

        llUnPay = (LinearLayout) findViewById(R.id.act_order_detail_ll_unPay);
        llPay = (LinearLayout) findViewById(R.id.act_order_detail_ll_pay);
        llSend = (LinearLayout) findViewById(R.id.act_order_detail_ll_send);
        llCancel = (LinearLayout) findViewById(R.id.act_order_detail_ll_cancel);
        llFinish = (LinearLayout) findViewById(R.id.act_order_detail_ll_finish);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.act_order_detail_btn_back:
                // TODO: 2016/11/26 返回键
                onBackPressed();

                break;
            case R.id.act_order_detail_btn_service:
                // TODO: 2016/11/26 售后
                break;

            case R.id.act_order_detail_tv_shopName:
                // TODO: 2016/11/26 点击店铺
                break;

        }
    }

    private void setUnPay() {

        llUnPay.setVisibility(View.VISIBLE);
        llPay.setVisibility(View.GONE);
        llSend.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llFinish.setVisibility(View.GONE);

    }

    private void setPay() {

        llUnPay.setVisibility(View.GONE);
        llPay.setVisibility(View.VISIBLE);
        llSend.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llFinish.setVisibility(View.GONE);

    }

    private void setSend() {

        llUnPay.setVisibility(View.GONE);
        llPay.setVisibility(View.GONE);
        llSend.setVisibility(View.VISIBLE);
        llCancel.setVisibility(View.GONE);
        llFinish.setVisibility(View.GONE);

    }

    private void setCancel() {

        llUnPay.setVisibility(View.GONE);
        llPay.setVisibility(View.GONE);
        llSend.setVisibility(View.GONE);
        llCancel.setVisibility(View.VISIBLE);
        llFinish.setVisibility(View.GONE);

    }

    private void setFinish() {

        llUnPay.setVisibility(View.GONE);
        llPay.setVisibility(View.GONE);
        llSend.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llFinish.setVisibility(View.VISIBLE);

    }
}
